package com.lxs.legou.order.interceptor;

import com.lxs.legou.order.utils.AdminToken;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @Auther: Mcb
 * @Date: 2022/1/16 16:50
 * @Description: feing拦截器
 */
@Component
public class MyFeignInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
//        //获得当前请求头，传递给商品微服务,获取的是当前线程的request信息，这时如果使用线程隔离，需要采用信号量隔离方案否则报错
//        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        if (requestAttributes !=null){
//            //获取请求头向下传
//            HttpServletRequest request = requestAttributes.getRequest();
//            Enumeration<String> headerNames = request.getHeaderNames();
//            while (headerNames.hasMoreElements()){
//                String name = headerNames.nextElement();
//                String value = request.getHeader(name);
//                System.out.println(name+"------"+value);
//                //把请求头传递给feign
//                requestTemplate.header(name,value);
//            }
//        }

        // MQ监听器中使用此方式
        String token = null;

        try {
            token = AdminToken.adminToken();
        } catch (IOException e) {
            e.printStackTrace();
        }

        requestTemplate.header("Authorization","Bearer "+token);
    }
}
